{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/11t-lAFP-wvQXI6G-sPrOnBUvmJlD0fT6?usp=sharing)"
      ],
      "metadata": {
        "id": "pNpHQn6FlCL1"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Claude 3.5 Sonnet vs GPT 4o - Portkey"
      ],
      "metadata": {
        "id": "ynEbjiyQlJat"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Portkey simplifies this process significantly. As an open-source [**AI Gateway**](https://github.com/Portkey-AI/gateway), Portkey provides a streamlined solution to manage access to over 250 LLMs through a single, unified API. This not only saves time and reduces complexity but also offers valuable insights into cost, performance, and accuracy metrics.\n",
        "\n",
        "With Portkey, you can efficiently compare and evaluate a wide range of LLMs, ensuring you choose the best model for your needs."
      ],
      "metadata": {
        "id": "bUQdnOHYqWLj"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "In this notebook, we illustrate how to run Top 10 LMSYS models with a single unified API using Portkey."
      ],
      "metadata": {
        "id": "0emCxX1Algw0"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quickstart"
      ],
      "metadata": {
        "id": "a7sDiU-IGzEm"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "KldJobxHjBNu",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "b10fc7d5-2fd2-4060-a5db-dcadfb16d94c"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m92.7/92.7 kB\u001b[0m \u001b[31m1.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m327.4/327.4 kB\u001b[0m \u001b[31m8.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m9.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.7/12.7 MB\u001b[0m \u001b[31m58.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m10.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m7.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip install -qU portkey-ai openai"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "To start, get your Portkey API key by signing up [here](https://app.portkey.ai/)\n",
        "\n",
        "- Go to \"Settings\" -> \"API Keys\" -> Create an API key"
      ],
      "metadata": {
        "id": "u281LJpvOhjv"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Using Portkey to run multiple LLMs"
      ],
      "metadata": {
        "id": "tA9Piq_tHYAt"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from openai import OpenAI\n",
        "from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders\n",
        "from tabulate import tabulate\n",
        "\n",
        "top_10_models = [\n",
        "    [\"gpt-4o-2024-05-13\", \"openai\"],\n",
        "    [\"claude-3-5-sonnet-20240620\", \"anthropic\"],\n",
        "]\n",
        "\n",
        "# Replace this with your own virtual keys\n",
        "virtual_keys = {\n",
        "     \"openai\": \"openai-7dc18e\",\n",
        "    \"anthropic\": \"claude3-5d8912\",\n",
        "}"
      ],
      "metadata": {
        "id": "ZPlY4GC1sBHK"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import userdata\n",
        "\n",
        "def run_models(prompt):\n",
        "    outputs = {}\n",
        "\n",
        "    for model, provider in top_10_models:\n",
        "        portkey = OpenAI(\n",
        "            api_key=\"dummy_key\",\n",
        "            base_url=PORTKEY_GATEWAY_URL,\n",
        "            default_headers=createHeaders(\n",
        "                api_key= userdata.get(\"PORTKEY_API_KEY\"),  # get your key here: https://app.portkey.ai/\n",
        "                virtual_key=virtual_keys[provider]\n",
        "            )\n",
        "        )\n",
        "\n",
        "        try:\n",
        "            response = portkey.chat.completions.create(\n",
        "                messages=[{\"role\": \"user\", \"content\": prompt}],\n",
        "                model=model,\n",
        "                max_tokens=256\n",
        "            )\n",
        "            outputs[model] = response.choices[0].message.content\n",
        "        except Exception as e:\n",
        "            outputs[model] = f\"Error: {str(e)}\"\n",
        "\n",
        "    return outputs\n",
        "\n",
        "def print_model_outputs(prompts):\n",
        "    table_data = []\n",
        "\n",
        "    for prompt in prompts:\n",
        "        outputs = run_models(prompt)\n",
        "        row = [prompt] + [outputs.get(model, \"N/A\") for model, _ in top_10_models]\n",
        "        table_data.append(row)\n",
        "\n",
        "    headers = [\"Prompt\"] + [model for model, _ in top_10_models]\n",
        "    table = tabulate(table_data, headers, tablefmt=\"grid\", maxcolwidths=[None] + [None] * len(top_10_models))\n",
        "    print(table)\n",
        "    print()\n",
        "\n",
        "# Example usage\n",
        "prompts = [\n",
        "    # Mathematical Reasoning:\n",
        "    \"Solve the following problem and explain your reasoning step-by-step: In how many ways can 8 people be seated around a circular table, considering rotations of the same arrangement as identical?\",\n",
        "\n",
        "    # Physical Reasoning:\n",
        "    \"Explain how a pulley system works to lift heavy objects, and calculate the mechanical advantage if there are 3 pulleys in the system. Include a discussion of the forces involved.\",\n",
        "\n",
        "    # Code Generation:\n",
        "    \"Write a Python function that implements the bubble sort algorithm. Then, analyze its time complexity and suggest how it could be optimized.\",\n",
        "\n",
        "    # Theory of Mind:\n",
        "    \"In the classic 'Sally-Anne' false belief task, describe how a child's response might differ at age 3 versus age 5. Explain the cognitive developments that account for this change.\",\n",
        "\n",
        "    # Indirect Reasoning:\n",
        "    \"You have three light switches in one room, each connected to a different lamp in an adjacent room. You can't see the lamps from where the switches are, and you can only enter the lamp room once. How would you determine which switch controls which lamp?\"\n",
        "]\n",
        "\n",
        "print_model_outputs(prompts)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "szT86PXzz1ib",
        "outputId": "130925fa-fd3e-4260-c3ca-f1f85e9dfeb1"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| Prompt                                                                                                                                                                                                                                                       | gpt-4o-2024-05-13                                                                                                                                                                                                                                                                                                                                                                                 | claude-3-5-sonnet-20240620                                                                                                                                                                                                                                                                                                                                                                                                                             |\n",
            "+==============================================================================================================================================================================================================================================================+===================================================================================================================================================================================================================================================================================================================================================================================================+========================================================================================================================================================================================================================================================================================================================================================================================================================================================+\n",
            "| Solve the following problem and explain your reasoning step-by-step: In how many ways can 8 people be seated around a circular table, considering rotations of the same arrangement as identical?                                                            | When solving the problem of determining the number of ways to seat 8 people around a circular table, considering that rotations of the same arrangement are identical, we need to take into account that circular permutations are different from linear permutations.                                                                                                                            | Let's approach this problem step-by-step:                                                                                                                                                                                                                                                                                                                                                                                                              |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | 1. **Understanding Circular Permutations**:                                                                                                                                                                                                                                                                                                                                                       | 1) First, we need to understand what the question is asking:                                                                                                                                                                                                                                                                                                                                                                                           |\n",
            "|                                                                                                                                                                                                                                                              |    - In a **linear arrangement**, the number of ways to arrange \\( n \\) people is \\( n! \\).                                                                                                                                                                                                                                                                                                       |    - We have 8 people to be seated.                                                                                                                                                                                                                                                                                                                                                                                                                    |\n",
            "|                                                                                                                                                                                                                                                              |    - For a **circular arrangement**, rotations result in the same arrangement, so we must adjust the linear permutation count to account for these identical rotations.                                                                                                                                                                                                                           |    - The seating is around a circular table.                                                                                                                                                                                                                                                                                                                                                                                                           |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |    - Rotations of the same arrangement are considered identical.                                                                                                                                                                                                                                                                                                                                                                                       |\n",
            "|                                                                                                                                                                                                                                                              | 2. **Adjusting for Circular Permutations**:                                                                                                                                                                                                                                                                                                                                                       |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |    - When fixing one person in position, the rest \\( (n-1) \\) people can be arranged in any order around that person.                                                                                                                                                                                                                                                                             | 2) For linear arrangements (like a straight line), we would use 8! (8 factorial). However, this is a circular arrangement, which changes our approach.                                                                                                                                                                                                                                                                                                 |\n",
            "|                                                                                                                                                                                                                                                              |    - Thus, for \\( n \\) people, we only consider the permutations of the remaining \\( n-1 \\) people.                                                                                                                                                                                                                                                                                               |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | 3) In a circular arrangement, rotations are considered the same. For example, if we have people A, B, C, D in a circle, these are all considered the same arrangement:                                                                                                                                                                                                                                                                                 |\n",
            "|                                                                                                                                                                                                                                                              | 3. **Calculation for 8 People**:                                                                                                                                                                                                                                                                                                                                                                  |    ABCD, BCDA, CDAB, DABC                                                                                                                                                                                                                                                                                                                                                                                                                              |\n",
            "|                                                                                                                                                                                                                                                              |    - Fix one person in place. We now have 7 people left to arrange, so we must calculate the permutations of these 7 individuals.                                                                                                                                                                                                                                                                 |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |    - The number of ways to arrange 7 people in a linear sequence is \\( 7! \\).                                                                                                                                                                                                                                                                                                                     | 4) To account for this, we can fix the position of one person. Once we do this, the problem becomes arranging the remaining 7 people.                                                                                                                                                                                                                                                                                                                  |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | 4. **Factorial Calculation**:                                                                                                                                                                                                                                                                                                                                                                     | 5) So, we can think of this as:                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |    - \\( 7! =                                                                                                                                                                                                                                                                                                                                                                                      |    - Fix one person (say, person A) at one position.                                                                                                                                                                                                                                                                                                                                                                                                   |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |    - Arrange the remaining 7 people in the other 7 positions.                                                                                                                                                                                                                                                                                                                                                                                          |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | 6) The number of ways to arrange 7 people in 7                                                                                                                                                                                                                                                                                                                                                                                                         |\n",
            "+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| Explain how a pulley system works to lift heavy objects, and calculate the mechanical advantage if there are 3 pulleys in the system. Include a discussion of the forces involved.                                                                           | A pulley system works by distributing the required force to lift a heavy object over multiple ropes and pulleys, thereby reducing the amount of force that needs to be applied by a user. Pulley systems come in different configurations, often classified as single movable pulleys, fixed pulleys, and combinations thereof, which can be referred to as compound or block and tackle systems. | A pulley system is a simple machine that uses wheels and ropes to lift heavy objects with less effort than would be required to lift them directly. Here's an explanation of how a pulley system works, along with a discussion of the forces involved and a calculation of the mechanical advantage for a system with three pulleys.                                                                                                                  |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | ### Basic Working Principle of Pulley Systems                                                                                                                                                                                                                                                                                                                                                     | How a Pulley System Works:                                                                                                                                                                                                                                                                                                                                                                                                                             |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | 1. **Fixed Pulley**: A fixed pulley is anchored in place and changes the direction of the force applied. It doesn't provide a mechanical advantage on its own, but it allows the user to apply force in a more convenient direction.                                                                                                                                                              | 1. Basic principle: A pulley system redistributes the force needed to lift an object by changing the direction of the applied force and potentially reducing the amount of force required.                                                                                                                                                                                                                                                             |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | 2. **Movable Pulley**: A movable pulley is attached to the load, and it moves with the load when lifting. This type of pulley halves the amount of input force needed because it effectively distributes the weight of the load over two segments of the rope.                                                                                                                                    | 2. Components: A basic pulley system consists of one or more wheels (pulleys) with a rope or cable running over them.                                                                                                                                                                                                                                                                                                                                  |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | 3. **Compound Pulley (Block and Tackle)**: This system consists of both fixed and movable pulleys. By combining pulleys, the mechanical advantage increases, meaning less input force is needed to lift the load.                                                                                                                                                                                 | 3. Fixed and movable pulleys: Pulley systems can include fixed pulleys (attached to a stable surface) and movable pulleys (attached to the load being lifted).                                                                                                                                                                                                                                                                                         |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | ### Forces Involved                                                                                                                                                                                                                                                                                                                                                                               | 4. Force distribution: As the rope passes over each pulley, it changes direction and distributes the force required to lift the load.                                                                                                                                                                                                                                                                                                                  |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | - **Input Force (Effort)**: This is the force applied by the user to lift the load.                                                                                                                                                                                                                                                                                                               | Forces Involved:                                                                                                                                                                                                                                                                                                                                                                                                                                       |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | 1. Gravity (Weight): The downward force exerted by the object being lifted due to gravity.                                                                                                                                                                                                                                                                                                                                                             |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | 2. Tension: The force applied to the rope or cable throughout the system.                                                                                                                                                                                                                                                                                                                                                                              |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | 3.                                                                                                                                                                                                                                                                                                                                                                                                                                                     |\n",
            "+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| Write a Python function that implements the bubble sort algorithm. Then, analyze its time complexity and suggest how it could be optimized.                                                                                                                  | Certainly! Below is a Python implementation of the bubble sort algorithm:                                                                                                                                                                                                                                                                                                                         | Certainly! Here's a Python function that implements the bubble sort algorithm:                                                                                                                                                                                                                                                                                                                                                                         |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | ```python                                                                                                                                                                                                                                                                                                                                                                                         | ```python                                                                                                                                                                                                                                                                                                                                                                                                                                              |\n",
            "|                                                                                                                                                                                                                                                              | def bubble_sort(arr):                                                                                                                                                                                                                                                                                                                                                                             | def bubble_sort(arr):                                                                                                                                                                                                                                                                                                                                                                                                                                  |\n",
            "|                                                                                                                                                                                                                                                              |     n = len(arr)                                                                                                                                                                                                                                                                                                                                                                                  |     n = len(arr)                                                                                                                                                                                                                                                                                                                                                                                                                                       |\n",
            "|                                                                                                                                                                                                                                                              |     for i in range(n):                                                                                                                                                                                                                                                                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |         # Track if any swapping happened in this pass                                                                                                                                                                                                                                                                                                                                             |     for i in range(n):                                                                                                                                                                                                                                                                                                                                                                                                                                 |\n",
            "|                                                                                                                                                                                                                                                              |         swapped = False                                                                                                                                                                                                                                                                                                                                                                           |         # Flag to optimize the algorithm                                                                                                                                                                                                                                                                                                                                                                                                               |\n",
            "|                                                                                                                                                                                                                                                              |         for j in range(0, n - i - 1):                                                                                                                                                                                                                                                                                                                                                             |         swapped = False                                                                                                                                                                                                                                                                                                                                                                                                                                |\n",
            "|                                                                                                                                                                                                                                                              |             if arr[j] > arr[j + 1]:                                                                                                                                                                                                                                                                                                                                                               |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |                 # Swap the elements                                                                                                                                                                                                                                                                                                                                                               |         for j in range(0, n-i-1):                                                                                                                                                                                                                                                                                                                                                                                                                      |\n",
            "|                                                                                                                                                                                                                                                              |                 arr[j], arr[j + 1] = arr[j + 1], arr[j]                                                                                                                                                                                                                                                                                                                                           |             # Compare adjacent elements                                                                                                                                                                                                                                                                                                                                                                                                                |\n",
            "|                                                                                                                                                                                                                                                              |                 swapped = True                                                                                                                                                                                                                                                                                                                                                                    |             if arr[j] > arr[j+1]:                                                                                                                                                                                                                                                                                                                                                                                                                      |\n",
            "|                                                                                                                                                                                                                                                              |         # If no two elements were swapped by inner loop, then break                                                                                                                                                                                                                                                                                                                               |                 # Swap if they are in the wrong order                                                                                                                                                                                                                                                                                                                                                                                                  |\n",
            "|                                                                                                                                                                                                                                                              |         if not swapped:                                                                                                                                                                                                                                                                                                                                                                           |                 arr[j], arr[j+1] = arr[j+1], arr[j]                                                                                                                                                                                                                                                                                                                                                                                                    |\n",
            "|                                                                                                                                                                                                                                                              |             break                                                                                                                                                                                                                                                                                                                                                                                 |                 swapped = True                                                                                                                                                                                                                                                                                                                                                                                                                         |\n",
            "|                                                                                                                                                                                                                                                              |     return arr                                                                                                                                                                                                                                                                                                                                                                                    |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | ```                                                                                                                                                                                                                                                                                                                                                                                               |         # If no swapping occurred, array is already sorted                                                                                                                                                                                                                                                                                                                                                                                             |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |         if not swapped:                                                                                                                                                                                                                                                                                                                                                                                                                                |\n",
            "|                                                                                                                                                                                                                                                              | ### Time Complexity Analysis                                                                                                                                                                                                                                                                                                                                                                      |             break                                                                                                                                                                                                                                                                                                                                                                                                                                      |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | **Worst-case Time Complexity:** \\(O(n^2)\\)                                                                                                                                                                                                                                                                                                                                                        |     return arr                                                                                                                                                                                                                                                                                                                                                                                                                                         |\n",
            "|                                                                                                                                                                                                                                                              | - When the array is reversely sorted.                                                                                                                                                                                                                                                                                                                                                             |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | - The algorithm does \\(n(n-1)/2\\) comparisons in the worst case.                                                                                                                                                                                                                                                                                                                                  | # Example usage                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | arr = [64, 34, 25, 12, 22, 11, 90]                                                                                                                                                                                                                                                                                                                                                                                                                     |\n",
            "|                                                                                                                                                                                                                                                              | **Best-case Time Complexity:** \\(O(n)\\)                                                                                                                                                                                                                                                                                                                                                           | sorted_arr = bubble_sort(arr)                                                                                                                                                                                                                                                                                                                                                                                                                          |\n",
            "|                                                                                                                                                                                                                                                              | - When the array is already sorted.                                                                                                                                                                                                                                                                                                                                                               | print(\"Sorted array:\", sorted_arr)                                                                                                                                                                                                                                                                                                                                                                                                                     |\n",
            "|                                                                                                                                                                                                                                                              | - The algorithm can use a flag (`swapped`) to determine if the array is already sorted, and then it only performs \\(O(n)\\) comparisons.                                                                                                                                                                                                                                                           | ```                                                                                                                                                                                                                                                                                                                                                                                                                                                    |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | ### Optimization Suggestions                                                                                                                                                                                                                                                                                                                                                                      | Time Complexity Analysis:                                                                                                                                                                                                                                                                                                                                                                                                                              |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | 1. **Early Exit Optimization**:                                                                                                                                                                                                                                                                                                                                                                   | 1. Worst-case time complexity: O(n^2)                                                                                                                                                                                                                                                                                                                                                                                                                  |\n",
            "|                                                                                                                                                                                                                                                              |    - As noted above, if during a pass                                                                                                                                                                                                                                                                                                                                                             |    - This occurs when the array is in reverse order, and                                                                                                                                                                                                                                                                                                                                                                                               |\n",
            "+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| In the classic 'Sally-Anne' false belief task, describe how a child's response might differ at age 3 versus age 5. Explain the cognitive developments that account for this change.                                                                          | The classic 'Sally-Anne' false belief task is a psychological test used to assess a child's theory of mind—the ability to attribute mental states to oneself and others. Theory of mind involves understanding that others can have beliefs, desires, intentions, and perspectives that are different from one's own.                                                                             | The Sally-Anne false belief task is a classic experiment in developmental psychology used to assess a child's theory of mind - their ability to understand that others have beliefs, desires, and intentions that are different from their own. In this task, children are presented with a scenario involving two dolls, Sally and Anne, and are asked to predict where Sally will look for her marble after it has been moved without her knowledge. |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | Here’s a brief description of the task: A child is introduced to two dolls, Sally and Anne. Sally has a basket and Anne has a box. Sally places a marble in her basket and then leaves the room. While Sally is away, Anne takes the marble from the basket and puts it into her box. Sally then returns, and the child is asked where Sally will look for her marble.                            | Here's how responses typically differ between 3-year-olds and 5-year-olds:                                                                                                                                                                                                                                                                                                                                                                             |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | **Responses at Different Ages:**                                                                                                                                                                                                                                                                                                                                                                  | Response at Age 3:                                                                                                                                                                                                                                                                                                                                                                                                                                     |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | At this age, most children will fail the false belief task. When asked where Sally will look for her marble, they tend to say that Sally will look in the new location where Anne moved it, even though Sally didn't see this happen. This incorrect answer suggests that 3-year-olds struggle to separate their own knowledge from what Sally knows.                                                                                                  |\n",
            "|                                                                                                                                                                                                                                                              | 1. **At Age 3:**                                                                                                                                                                                                                                                                                                                                                                                  |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |    - A 3-year-old child typically fails the task. When asked where Sally will look for the marble, the child is likely to say that Sally will look in Anne's box, where the marble actually is.                                                                                                                                                                                                   | Response at Age 5:                                                                                                                                                                                                                                                                                                                                                                                                                                     |\n",
            "|                                                                                                                                                                                                                                                              |    - This response suggests that the child does not yet understand that Sally holds a false belief—that Sally does not know what the child knows (i.e., that the marble has been moved).                                                                                                                                                                                                          | By age 5, most children will pass the false belief task. They correctly predict that Sally will look in the original location where she left the marble, understanding that Sally's belief is different from reality and from their own knowledge.                                                                                                                                                                                                     |\n",
            "|                                                                                                                                                                                                                                                              |    - Cognitive Development: At this age, children are usually egocentric, a term coined by                                                                                                                                                                                                                                                                                                        |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | Cognitive Developments Accounting for this Change:                                                                                                                                                                                                                                                                                                                                                                                                     |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | 1.                                                                                                                                                                                                                                                                                                                                                                                                                                                     |\n",
            "+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| You have three light switches in one room, each connected to a different lamp in an adjacent room. You can't see the lamps from where the switches are, and you can only enter the lamp room once. How would you determine which switch controls which lamp? | To determine which switch controls which lamp, you can use the following method:                                                                                                                                                                                                                                                                                                                  | This is a classic puzzle that requires some creative thinking! Here's how you can solve it:                                                                                                                                                                                                                                                                                                                                                            |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | 1. **Label the switches A, B, and C** for reference.                                                                                                                                                                                                                                                                                                                                              | 1. Start with all switches in the off position.                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | 2. **Turn on switch A** and leave it on for a few minutes. This will allow the lamp controlled by switch A to warm up.                                                                                                                                                                                                                                                                            | 2. Turn on the first switch and leave it on for about 10 minutes.                                                                                                                                                                                                                                                                                                                                                                                      |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | 3. **After a few minutes, turn off switch A and turn on switch B**. Leave switch B on.                                                                                                                                                                                                                                                                                                            | 3. After 10 minutes, turn off the first switch and immediately turn on the second switch.                                                                                                                                                                                                                                                                                                                                                              |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | 4. **Leave switch C off**.                                                                                                                                                                                                                                                                                                                                                                        | 4. Quickly enter the room with the lamps.                                                                                                                                                                                                                                                                                                                                                                                                              |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | 5. Now, **go into the adjacent room** with the lamps.                                                                                                                                                                                                                                                                                                                                             | Now, here's how you can determine which switch controls which lamp:                                                                                                                                                                                                                                                                                                                                                                                    |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | - The lamp that is **on** is controlled by switch B.                                                                                                                                                                                                                                                                                                                                              | 1. The lamp that is on is controlled by the second switch (the one you just turned on before entering the room).                                                                                                                                                                                                                                                                                                                                       |\n",
            "|                                                                                                                                                                                                                                                              | - The lamp that is **off and warm** is controlled by switch A.                                                                                                                                                                                                                                                                                                                                    |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | - The lamp that is **off and cool** is controlled by switch C.                                                                                                                                                                                                                                                                                                                                    | 2. Among the two lamps that are off, feel them. The lamp that is warm but not lit is controlled by the first switch (the one you had on for 10 minutes and then turned off).                                                                                                                                                                                                                                                                           |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              | By using the combination of light, warmth, and the state of the lamps, you can accurately determine which switch controls which lamp.                                                                                                                                                                                                                                                             | 3. The lamp that is off and cool to the touch is controlled by the third switch (the one you never turned on).                                                                                                                                                                                                                                                                                                                                         |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | This solution works because:                                                                                                                                                                                                                                                                                                                                                                                                                           |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | - Incandescent bulbs get warm when they're on for a while.                                                                                                                                                                                                                                                                                                                                                                                             |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | - LED or CFL bulbs may not get as warm, but they will still be warmer than a bulb that was never turned on.                                                                                                                                                                                                                                                                                                                                            |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "|                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                   | By using                                                                                                                                                                                                                                                                                                                                                                                                                                               |\n",
            "+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "\n"
          ]
        }
      ]
    }
  ]
}